package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login/test1")
public class LoginServlet extends BaseServlet {


    private void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面参数信息
        String username =req.getParameter("username");
        String pwd =req.getParameter("pwd");
        if("zhaosi".equals(username)&&"666".equals(pwd)){
           Cookie cookieName =new Cookie("username",username);
           Cookie cookiePwd =new Cookie("pwd",pwd);
           //获取页面记住状态复选框的值
           String ck =req.getParameter("ck");
           if(ck!=null){
               //设置存活时间
               cookieName.setMaxAge(7*24*60*60);
               cookiePwd.setMaxAge(7*24*60*60);
               req.getSession().setAttribute("ck","checked");
           }else{
               //不记住
               //设置存活时间
               cookieName.setMaxAge(0);
               cookiePwd.setMaxAge(0);
               req.getSession().removeAttribute("ck");
           }
           //设置cookie存储路径：设置在根路径，将来页面获取就不需要考虑存储的路径
           cookieName.setPath("/");
           cookiePwd.setPath("/");
           resp.addCookie(cookieName);
           resp.addCookie(cookiePwd);
           resp.sendRedirect(req.getContextPath()+"/page/success.jsp");
        }else{
            //登陆失败
            resp.sendRedirect(req.getContextPath()+"/page/login.jsp");
        }
    }
}
